<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Exceptions\BasicException;  //引入框架异常处理类
use Illuminate\Support\Facades\Hash;    //引入hash
use App\Services\Cache\AdminCacheService;   //admin缓存
use App\Services\Tools\JwtService;    //引入JWT服务
use App\Services\Tools\FileService;   //引入文件处理类
use App\Response\Response;

class GradeConfigController extends BaseController
{

    /**
     * @api {post} vip/gradeconfig/rank/list 等级列表
     * @apiVersion 0.1.0
     * @apiName 等级列表
     * @apiGroup vip管理
     *
     * @apiParam {int} grade_big_id 大等级id，必传
     *
     * @apiSuccessExample 成功响应
     *
     *
     *{
    "code": 200,
    "data": [{
    "id": 17,
    "grade(等级)": "17",
    "experience(经验值)": "10000.0",
    "head_picture(标签图)": "http://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/admin_images/5fd888cf11faf.png",
    "name(等级名称)": "海将Ⅶ",
    "grade_big_id": 2,
    "reward_id": 148,
    "screen_img(公屏图)": "http://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/admin_images/5fd888cf11faf.png",
    "reward(礼包)": {
    "id(礼包ID)": 148,
    "type(礼包类型 1普通道具2特殊道具)": 1
    }
    }],
    "message": ""
    }
     *
     */
    public function GradeConfigList(Request $request)
    {
        $param = $request->only('grade_big_id');
        $v = app('validator')->make($param, [
            'grade_big_id' => 'filled|integer|min:0',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $list = app('repo_grade_config')->GradeConfigList($param);
        return Response::sendData($list);
    }

    /**
     * @api {post} vip/gradeconfig/reward/list 添加等级礼包列表
     * @apiVersion 0.1.0
     * @apiName 添加等级礼包列表
     * @apiGroup vip管理
     *
     *
     * @apiSuccessExample 成功响应
     *
     *
     *{
    "code": 200,
    "data": [{
    "id": 151,
    "name（名称）": "7日签到礼"
    }],
    "message": ""
    }
     *
     */
    public function rewardList(Request $request)
    {
        $list = app('repo_grade_config')->getRewardList();
        return Response::sendData($list);
    }

    /**
     * @api {post} vip/gradeconfig/rank/add  添加等级
     * @apiVersion 0.1.0
     * @apiName 添加等级
     * @apiGroup vip管理
     *
     * @apiParam {int} grade_big_id 大等级id，必传
     * @apiParam {number} grade 等级,必传
     * @apiParam {string} name 等级名称,必传
     * @apiParam {string} experience 经验值,必传
     * @apiParam {file} head_picture  爵位图片,必传
     * @apiParam {int} reward_id 礼包id，可选
     * @apiParam {int} screen_img 公屏图，必传
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 93010  爵位添加失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93010,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function rankAdd(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'grade' => 'required',
                'experience' => 'required|numeric',
                'head_picture' => 'required',
                'grade_big_id' => 'integer|min:0',
                'name' => 'string|max:100',
                'reward_id' => 'integer|min:0',
                'screen_img' => 'string|max:255',
            ],
            [
                'grade.required' => '等级不能为空',
//                'grade.numeric' => '等级必须为数字',
                'experience.required' => '经验值不能为空',
                'experience.numeric' => '经验值必须为数字',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        if (strlen($request->input('grade')) > 11) {
            throw new BasicException(10001, '等级字符长度不能大于11位');
        }
        if (strstr($request->input('experience'), '.')) {
            if (strlen($request->input('experience')) > 12) {
                throw new BasicException(10001, '经验值字符长度不能大于12位');
            }
        } else {
            if (strlen($request->input('experience')) > 10) {
                throw new BasicException(10001, '经验值字符长度不能大于10位');
            }
        }
        $grade = $request->input('grade');
        $experience = $request->input('experience');
        $grade_big_id = $request->input('grade_big_id',0);
        $name = $request->input('name','');
        $reward_id = $request->input('reward_id',0);
        $screen_img = $request->input('screen_img','');
        //对头像的处理
        $param['head_picture'] = $request->input('head_picture');
        //数据组合
        $param['grade']=$grade;
        $param['experience']=$experience;
        $param['grade_big_id'] = $grade_big_id;
        $param['name'] = $name;
        if($reward_id) {
            $isHave = app('model_reward')->where(['active' => 1, 'id' => $reward_id])->first();
            if (!$isHave) {
                throw new BasicException(10001, '礼包id不存在');
            }
            $param['reward_id'] = $reward_id;
        }
        $param['screen_img'] = $screen_img;
        //执行添加操作
        app('repo_grade_config')->rankAdd($param);

        app('redis')->del('grade:config');

        return Response::sendData(true);
    }




    /**
     * @api {post} vip/gradeconfig/rank/save  编辑等级
     * @apiVersion 0.1.0
     * @apiName 编辑等级
     * @apiGroup vip管理
     * @apiParam {int} grade_big_id 大等级id，必传
     * @apiParam {number} id 主键，必传
     * @apiParam {number} grade 等级,必传
     * @apiParam {string} name 等级名称,必传
     * @apiParam {string} experience 经验值,必传
     * @apiParam {file} head_picture  爵位图片,必传
     * @apiParam {int} reward_id 礼包id，可选
     * @apiParam {int} screen_img 公屏图，必传
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 93011  爵位编辑失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93011,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function rankSave(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required|numeric',
//                'grade' => 'required|numeric',
                'experience' => 'required|numeric',
                'head_picture' => 'required',
//                'grade_big_id' => 'required|integer|min:0',
                'name' => 'required|string|max:100',
                'reward_id' => 'filled|integer|min:0',
                'screen_img' => 'required|string|max:255',
            ],
            [
                'grade.required' => '等级不能为空',
//                'grade.numeric' => '等级必须为数字',
                'experience.required' => '经验值不能为空',
                'experience.numeric' => '经验值必须为数字',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        if (strlen($request->input('grade')) > 11) {
            throw new BasicException(10001, '等级字符长度不能大于11位');
        }
        if (strstr($request->input('experience'), '.')) {
            if (strlen($request->input('experience')) > 12) {
                throw new BasicException(10001, '经验值字符长度不能大于12位');
            }
        } else {
            if (strlen($request->input('experience')) > 10) {
                throw new BasicException(10001, '经验值字符长度不能大于10位');
            }
        }
        $id = $request->input('id');
        $grade = $request->input('grade');
        $experience = $request->input('experience');
//        $grade_big_id = $request->input('grade_big_id');
        $name = $request->input('name');
        $reward_id = $request->input('reward_id');
        $screen_img = $request->input('screen_img');

        //数据组合
        $param['grade']=$grade;
        $param['experience']=$experience;
        $param['id']=$id;
        $param['head_picture'] = $request->input('head_picture');
//        $param['grade_big_id'] = $grade_big_id;
        $param['name'] = $name;
        if($reward_id) {
            $param['reward_id'] = $reward_id;
        } else {
            $param['reward_id'] = 0;
        }
        $param['screen_img'] = $screen_img;
        //执行添加操作
        app('repo_grade_config')->rankSave($param);

        app('redis')->del('grade:config');

        return Response::sendData(true);
    }

    /**
     * @api {post} vip/gradeconfig/rank/del  删除等级
     * @apiVersion 0.1.0
     * @apiName 删除等级
     * @apiGroup vip管理
     * @apiParam {string} ids 主键，多个用逗号隔开,必传
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 93012  爵位删除失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93012,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function rankdel(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'ids' => 'required',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $ids = $request->input('ids');
        $param = explode(',', $ids);
        //执行添加操作
        app('repo_grade_config')->rankDel($param);

        app('redis')->del('grade:config');
        return Response::sendData(true);
    }


    /**
     * @api {post} vip/gradeconfig/rank/detail  等级详情
     * @apiVersion 0.1.0
     * @apiName 等级详情
     * @apiGroup vip管理
     * @apiParam {string} id 主键,必传
     * @apiSuccessExample 成功响应
     *
     * {
    "code": 200,
    "data": {
    "id": 57,
    "grade(等级)": "17",
    "experience(经验值)": "10000.0",
    "head_picture(标签图)": "http://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/admin_images/5fd888cf11faf.png",
    "name(等级名称)": "海将Ⅶ",
    "grade_big_id": 2,
    "reward_id": 148,
    "screen_img(公屏图)": "http://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/admin_images/5fd888cf11faf.png",
    "reward(礼包)": {
    "id(礼包ID)": 148,
    "type(礼包类型 1普通道具2特殊道具)": 1
    }
    },
    "message": ""
    }
     *
     */
    public function detail(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required|numeric',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $id = $request->input('id');
        //执行查询操作
        $detail = app('repo_grade_config')->rankDetail($id);
        return Response::sendData($detail);
    }






}
